﻿using ETSModel;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UnitRandomMoveComponent : MonoBehaviour
{
  private Vector3 direction = Vector3.forward;

  public int speed = 10;

  // Use this for initialization
  void Start()
  {

  }

  // Update is called once per frame
  void Update()
  {
    transform.Translate(direction * speed * Time.deltaTime);
  }

  // 前方有障碍物
  private void OnTriggerEnter(Collider other)
  {
    direction = RandomDirection();
  }

  private Vector3 RandomDirection()
  {
    Vector3 vector3 = direction;
    System.Random random = new System.Random();
    int dir = random.Next(4);
    if (dir == 0)
    {
      vector3 = Vector3.forward;
    }
    else if (dir == 1)
    {
      vector3 = Vector3.back;
    }
    else if (dir == 2)
    {
      vector3 = Vector3.left;
    }
    else if (dir == 3)
    {
      vector3 = Vector3.right;
    }
    return vector3;
  }
}